<template>
  <MyDrawer ref="MyRef" title="地区设置" @submit="submit" :disabled="false">
    <el-form :model="drawerProps.rowData" :rules="rules" ref="formRef" label-width="120">
      <el-form-item label="地区名称" prop="name">
        <el-input v-model="drawerProps.rowData.name"></el-input>
      </el-form-item>
      <el-form-item label="排序值">
        <el-input-number v-model="drawerProps.rowData.sort" :min="1" :max="10000"></el-input-number>
      </el-form-item>
      <el-form-item label="经纬度">
        <el-input v-model="drawerProps.rowData.center" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="small" @click="open">选取中心点</el-button>
      </el-form-item>
      <el-form-item label="别名">
        <el-input v-model="drawerProps.rowData.alias"></el-input>
      </el-form-item>
      <el-form-item label="站点前缀">
        <el-input v-model="drawerProps.rowData.prefix"></el-input>
      </el-form-item>
    </el-form>
    <chooseCenter ref="centerRef" @select="getCenter"></chooseCenter>
  </MyDrawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ref } from "vue";
import { Region } from "@/api/interface/region";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import chooseCenter from "@/components/chooseCenter/index.vue";

const centerRef = ref<InstanceType<typeof chooseCenter> | null>();

const open = () => {
  centerRef.value?.open();
};

const getCenter = (val: any) => {
  drawerProps.value.rowData.center = val.location;
  drawerProps.value.rowData.longitude = val.location.split(",")[0];
  drawerProps.value.rowData.latitude = val.location.split(",")[1];
};

const MyRef = ref<InstanceType<typeof MyDrawer> | null>();

interface DrawerProps {
  rowData: Region.Option;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const rules: FormRules = {
  name: [
    { required: true, message: "地区不可为空", trigger: "blur" },
    { max: 15, min: 1, message: "字符在1-15之间" }
  ],
  preFix: [{ max: 10, min: 0, message: "站点前缀字数不可超过10个字" }],
  alias: [{ max: 8, min: 0, message: "字数不可超过8个字" }]
};

const initData: Region.Option = {
  name: "",
  level: "province",
  center: "",
  longitude: 0,
  latitude: 0,
  pid: 1,
  sort: 1000,
  prefix: "",
  alias: ""
};

const drawerProps = ref<DrawerProps>({
  rowData: JSON.parse(JSON.stringify(initData))
});

const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerProps.value.rowData = JSON.parse(JSON.stringify(params.rowData));
  MyRef.value?.acceptParams();
};

const formRef = ref<FormInstance>();

const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (valid) {
      try {
        await drawerProps.value.api!(drawerProps.value.rowData);
        ElMessage.success("成功");
        await drawerProps.value.getTable!();
        MyRef.value?.close();
      } catch (error) {}
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style></style>
